﻿

local moduleName = "LeaderIcon"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Leader Icons",
}

if GetLocale() == "zhCN" then
	L[moduleName] = "队长图标"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "隊長圖示"
end

----------------------------------
--      Local Declaration      --
----------------------------------



----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName)

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
	}
}

plugin.defaultDB = {
	profile = {
		iconsize = 12,
		style = {
			position = "TOPLEFT",
			xof = 22,
			yof = 6,
		}
	}
}


plugin.head = {
	core = true
}
plugin.moduletype = "icon"
----------------------------------
--      Module Functions        --
----------------------------------

function plugin:OnEnable()
	self:RegisterEvent("PARTY_LEADER_CHANGED", "UpdateAll")
	self:RegisterEvent("PARTY_LOOT_METHOD_CHANGED", "UpdateAll")
	self:RegisterEvent("PARTY_MEMBERS_CHANGED", "UpdateAll")

end


function plugin:OnCreate(frame)
	local icon = frame:CreateTexture(nil, "OVERLAY")
--	icon:SetPoint("CENTER", frame, "TOPLEFT", 22, 0)
	icon:SetHeight(self.db.profile.iconsize)
	icon:SetWidth(self.db.profile.iconsize)
	icon:Hide()

	local loot = frame:CreateTexture(nil, "OVERLAY")
	loot:SetHeight(self.db.profile.iconsize)
	loot:SetWidth(self.db.profile.iconsize)
	loot:SetTexture("Interface\\GroupFrame\\UI-Group-MasterLooter")
	loot:ClearAllPoints()
	loot:SetPoint("LEFT", icon, "RIGHT", 0, 0)
	loot:Hide()
	icon.masterlooter = loot

	return icon
end

function plugin:OnUpdate(frame)
if not UnitExists(frame.unit) then return end
	local icon = frame.parts[moduleName]
	local leader = false
	local mlooter = false
	local assistant = false
	local raidrank = 0

	if frame.unit == "player" then
		if IsPartyLeader() or IsRaidLeader() then
			leader = true
		elseif IsRaidOfficer() then
			assistant = true
		end

		local _, masterlooter = GetLootMethod()
		if masterlooter == 0 then
			mlooter = true
		end

	else
		local raid = frame.unit:match("^raid(%d%d?)$")
		if raid then
			local _, rank, _, _, _, _, _, _, _, role, isML = GetRaidRosterInfo(tonumber(raid))
			if rank == 2 then
				leader = true
			elseif rank == 1 then
				assistant = true
			end
			if isML then
				mlooter = true
			end

		else
			local party = frame.unit:match("^party(%d)$")
			if party then
				if GetPartyLeaderIndex() == tonumber(party) then
					leader = true
				end
			else
				leader = false
			end
		end
	end
	if leader then
		icon:SetTexture("Interface\\GroupFrame\\UI-Group-LeaderIcon")
		icon:Show()
	elseif assistant then
		icon:SetTexture("Interface\\GroupFrame\\UI-Group-AssistantIcon")
		icon:Show()
	else
		icon:Hide()
	end

	if mlooter then
		icon.masterlooter:Show()
	else
		icon.masterlooter:Hide()		
	end
end



----------------------------------
--      Option Handlers         --
----------------------------------
function plugin:SetIconSize()
	self:UpdateAllWith(function(frame)
		frame.parts[moduleName]:SetHeight(self.db.profile.iconsize)
		frame.parts[moduleName]:SetWidth(self.db.profile.iconsize)
		frame.parts[moduleName].masterlooter:SetHeight(self.db.profile.iconsize)
		frame.parts[moduleName].masterlooter:SetWidth(self.db.profile.iconsize)
	end)
end